<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList,teamCservlets.ApplicationBean,teamCservlets.PersonBean" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Team C Defect System</title>
</head>

<%-- 
First, get all of the session and request data this jsp requires.  Make sure all
the data is available before building the display, to avoid any null pointer exceptions.
--%>
<jsp:useBean id="user" type="teamCservlets.PersonBean" scope="session"/>
<jsp:useBean id="newDefect" type="teamCservlets.DefectBean" scope="session"/>
<%
ArrayList<ApplicationBean> applications = (ArrayList<ApplicationBean>)session.getAttribute("applicationList");
ArrayList<PersonBean> people = (ArrayList<PersonBean>)session.getAttribute("personList");

boolean error = false;

// The "user" attribute identifies the currently logged-in user, if any.  If it's not a valid
// user, force the user to login.
if ((user == null) || (user.getId() == -1)) { 
	// No valid user object in the session, so the user must login
	response.sendRedirect("login.jsp");
	error = true;
} 
// The newDefect attribute is a DefectBean which might have some fields
// already filled in with values, which will be used on the display.
if (newDefect == null) { 
	out.println("newDefect is null");
	error = true;
} 
// List of applications, used to populate the Application combo box.
if (applications == null) {
	out.println("applications is null");
	error = true;
}
// List of users, used to populate the Assignee combo box.
if (people == null) {
	out.println("people is null");
	error = true;
}
if (!error) {
%>

<body BGCOLOR="#FDF5E6">
	<H1 ALIGN="CENTER">Team C Defect System</H1>
	<p style="font-size:150%">Create a New Defect</p>
	<br>
	<form action="defectactions">

	<table style="width:100%">
	
	<%-- 
	Populate the Application combo box.
	This inserts the application ID into the "value" and the application name into the displayed 
	list item.  When the user selects an application and sends the form back to the servlet, 
	the "value" will be retrieved, and this will be the application ID. 
	--%>
	<tr>
	<td>Application:<i style="color:red"> *</i></td>
	<td><select name="selApplicationId" width="100%">
	<%
	for (ApplicationBean app : applications) {
	%>
		<option value="<%= app.getApplicationID() %>"

	<%-- If the newDefect has an appID already selected, then make that item the selected one in the combo box --%>
	<% if (newDefect.getApplicationID() == app.getApplicationID()) { %> selected <% } %> >
		<%= app.getApplicationName() %>
	<%
	} // end of for loop 
	%>
	</select></td></tr>

	<%-- 
	Populate the Priority combo box, using hard-coded values. 
	If the newDefect has a priority already selected, then make that item the selected one in the combo box.
	--%>
	<tr>
	<td>Priority:<i style="color:red"> *</i></td>
	<td><select name="selPriority" width="100%">
								<option value="LOW"
	<% if ((newDefect.getPriority() != null) && (newDefect.getPriority().equals("LOW"))) { %> selected <% } %> >Low
								<option value="MEDIUM"
	<% if ((newDefect.getPriority() != null) && (newDefect.getPriority().equals("MEDIUM"))) { %> selected <% } %> >Medium
								<option value="HIGH"
	<% if ((newDefect.getPriority() != null) && (newDefect.getPriority().equals("HIGH"))) { %> selected <% } %> >High
	</select></td></tr>


	<%-- 
	Populate the Assignee combo box.  This inserts the 
	person ID into the "value" and the person's first name + last name into the displayed   
	list item. When the user selects an assignee and sends the form back to the servlet,  
	the "value" will be retrieved, and this will be the person ID. Note, the first item in 
	the list is (none) and has an invalid person ID of -1.  The user doesn't have to select an 
	assignee when creating a new defect. So, the servlet must check for this. 
	--%>

	<tr><td>Assignee:</td>
		<td><select name="selAssigneeId" width="100%">	
			<option value="-1">(none)
	<%
	for (PersonBean person : people) {
	%>
			<option value="<%= person.getId() %>"

	<%-- If the newDefect has an assignee already selected, then make that item the selected one in the combo box --%>
	<% if (newDefect.getAssigneeID() == person.getId()) { %> selected <% } %> >
		<%= person.getFirstAndLastName() %>
	<%
	} // end of for loop 
	%>
	</select></td></tr>
	
	<%-- 
	Display the summary text box. If the newDefect has a summary already entered, 
	then display that text within the text box.
	--%>
	<tr>
	<td>Summary:<i style="color:red"> *</i></td>
	<td><input size="70" type="text" name="txtSummary"
	<% if (newDefect.getSummary() != null) { %>value="<%=newDefect.getSummary()%>" >
	<% } %> 
	</td></tr>
	</table>
	
	<table style="width:100%">
	<%-- 
	Display the description text area. If the newDefect has a summary already entered, 
	then display that text within the text area.
	--%>
	<tr>
	<td>Description (describe the problem and steps to recreate it):<i style="color:red"> *</i></td>
	</tr>
	<tr>
	<td align="left" style="width:100%">
	<textarea name="txtDescription" rows=10 cols=80 ><% if (newDefect.getDetailedDescription() != null) { %><%= newDefect.getDetailedDescription() %><% } %></textarea>
 	</td></tr>
	
	<%-- 
	Display the action buttons: Save to store the new defect, and Cancel to discard
	and return to the defect list page.
	--%>
	<tr>
		<td align="left">
		<input type="submit" name="btnStoreNewDefect" value="Save">
		<input type="submit" name="btnGoToHomepage" value="Cancel">
	</tr>
	</table>

	<p><i style="color:red">*</i> Required fields </p>
	
	<%-- 
	If there's a message attribute in the request, output the message. 
	--%>
	<%
	String message = (String)request.getAttribute("message");
	if (message != null) {
	%>
	<strong style="color:red;font-size:125%"><%= message %></strong>
	<%
	}
	%>

	</form>
</body>

<% } // end if (!error)
%>

</html>